<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
// import { GetFukaOption } from '@/api/modules/fukaact'
import { GetFukaOption } from '@/api/modules/fukaact'
import { GetFukaUserDetailDeta } from '@/api/modules/fukaactUser'
import PageNumber from '@/store/modules/PageNumberCache'

// const imgDomain = 'https://img1.eprhan.cc'
const route = useRoute()
// const router = useRouter()
const PageNumberCache = PageNumber()

// 查询表单
const findFrom = reactive({
  FukaActId: route.query.fukaActId as string,
  UserId: (route.query.UserId as string) ? route.query.UserId as string : '',
  ClassId: '~',
  FromOrderNo: '',
  FromUserId: '',
  Flag: '~',
})

const dataList: any = reactive({
  list: [],
})

const OptionList = reactive({
  fukaOption: [],
})

const loading = ref(true)
const total = ref(0)
let PageSize = 20

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.FukaActId && { FukaActId: findFrom.FukaActId }),
    ...(findFrom.UserId && { UserId: findFrom.UserId }),
    ...(findFrom.FromOrderNo && { FromOrderNo: findFrom.FromOrderNo }),
    ...(findFrom.FromUserId && { FromUserId: findFrom.FromUserId }),
    ...(findFrom.ClassId !== '~' && { ClassId: findFrom.ClassId }),
    ...(findFrom.Flag !== '~' && { Flag: findFrom.Flag }),
  }
  await GetFukaUserDetailDeta(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
    else {
      ElMessage.error(`查询失败。${dataRes.errors}`)
      loading.value = false
    }
  })
    .catch(() => {
      loading.value = false
    })
  loading.value = false
}

// 查询
function Inquire() {
  GetDataList(1, PageSize)
}

// function SkipPage(name: any) {
//   router.push({
//     name: `${name}`,
//     query: {
//       fukaActId: findFrom.FukaActId,
//     },
//   })
// }

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}
function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
  PageNumberCache.SetFukaUserDetailPage(val)
}

const nowPageNum = computed(() => {
  return PageNumberCache.GetFukaUserDetailPage()
})

onMounted(async () => {
  // 获取福卡活动
  const fukaData: any = await GetFukaOption()
  if (fukaData.succeeded) {
    OptionList.fukaOption = fukaData.data
    // console.log(route.query.fukaActId === undefined)

    if (route.query.fukaActId === undefined) {
      findFrom.FukaActId = fukaData.data[0].fukaActId
    }

    nextTick(() => {
      OptionList.fukaOption = fukaData.data
    })
  }

  if (findFrom.FukaActId) {
    GetDataList(PageNumberCache.GetFukaUserDetailPage())
  }
  else {
    ElMessage.error('请先选择福卡活动')
    loading.value = false
  }
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="福卡活动">
                <el-select v-model="findFrom.FukaActId" filterable placeholder="请选择福卡活动">
                  <el-option v-for="item in (OptionList.fukaOption as any[])" :key="item.fukaActId" :label="(item.fukaActId as any)" :value="item.fukaActId" />
                </el-select>
              </el-form-item>
              <el-form-item label="用户ID">
                <el-input v-model="findFrom.UserId" placeholder="请输入用户ID" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="来源">
                <el-select v-model="findFrom.ClassId" filterable placeholder="请选择来源">
                  <el-option label="全部" value="~" />
                  <el-option label="新客专享福卡派送" :value="10" />
                  <el-option label="好友助力抽福卡" :value="20" />
                  <el-option label="下单解锁抽福卡" :value="30" />
                  <el-option label="下单福卡随心兑" :value="31" />
                  <el-option label="好友福卡转增" :value="40" />
                  <el-option label="福利官派福" :value="50" />
                </el-select>
              </el-form-item>
              <el-form-item label="福卡状态">
                <el-select v-model="findFrom.Flag" filterable placeholder="请选择福卡状态">
                  <el-option label="全部" value="~" />
                  <el-option label="待领卡" :value="-2" />
                  <el-option label="待开卡" :value="-1" />
                  <el-option label="待兑换" :value="0" />
                  <el-option label="已兑换" :value="1" />
                  <el-option label="退款" :value="-20" />
                  <el-option label="下单退款" :value="-3" />
                  <el-option label="福卡转增" :value="-4" />
                  <el-option label="福卡转增作废" :value="-5" />
                </el-select>
              </el-form-item>
              <el-form-item label="来自订单">
                <el-input v-model="findFrom.FromOrderNo" placeholder="请输入来自订单" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="转赠人用户ID">
                <el-input v-model="findFrom.FromUserId" placeholder="请输入转赠人用户ID" @keyup.enter="Inquire" />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <!-- <div class="mb-5 text-right">

      </div> -->
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="100" />
        <el-table-column align="center" prop="fukaActId" label="福卡活动ID" width="100" />
        <el-table-column align="center" prop="subscriberInfo.userId" label="用户ID" width="100" />
        <!-- <el-table-column align="center" prop="subscriberInfo.scOpenId" label="OpenID" width="280" /> -->
        <el-table-column align="center" prop="subscriberInfo" label="用户信息" width="200">
          <template #default="scope">
            <div v-if="!scope.row.subscriberInfo">
              未授权
            </div>
            <el-tooltip
              v-else
              class="box-item"
              effect="dark"
              :content="scope.row.subscriberInfo?.scOpenId"
              placement="top"
            >
              <div class="user-name">
                <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.subscriberInfo?.userAvatar" />
                <div>
                  <p style="margin: 0;">
                    {{ scope.row.subscriberInfo?.userNickName }}
                  </p>
                  <span>
                    {{ scope.row.subscriberInfo?.userPhone }}
                  </span>
                </div>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" prop="fukaTagid" label="福卡ID" />
        <!-- <template #default="scope">
            {{ CardIdOption.find((item: any) => item.fukaCardId === scope.row.fukaTagid) }}
          </template>
        </el-table-column> -->
        <el-table-column align="center" prop="fromSubscriberInfo.userId" label="助力或转赠人ID" width="130" />
        <!-- <el-table-column align="center" prop="fromSubscriberInfo.scOpenId" label="转赠人OpenID" width="280" /> -->
        <el-table-column align="center" prop="fromSubscriberInfo" label="助力或转赠人信息" width="200">
          <template #default="scope">
            <div v-if="!scope.row.fromSubscriberInfo">
              未授权
            </div>
            <el-tooltip
              v-else
              class="box-item"
              effect="dark"
              :content="scope.row.fromSubscriberInfo?.scOpenId"
              placement="top"
            >
              <div class="user-name">
                <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.fromSubscriberInfo?.userAvatar" />
                <div>
                  <p style="margin: 0;">
                    {{ scope.row.fromSubscriberInfo?.userNickName }}
                  </p>
                  <span>
                    {{ scope.row.fromSubscriberInfo?.userPhone }}
                  </span>
                </div>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" width="240" prop="fukaCode" label="福卡编码" /> -->
        <el-table-column align="center" width="160" prop="classId" label="来源">
          <template #default="scope">
            <el-tag>
              {{ scope.row.classId === 10 ? '新客专享福卡派送'
                : scope.row.classId === 20 ? '好友助力抽福卡'
                  : scope.row.classId === 30 ? '下单解锁抽福卡'
                    : scope.row.classId === 31 ? '下单福卡随心兑'
                      : scope.row.classId === 40 ? '好友福卡转增'
                        : scope.row.classId === 50 ? '福利官派福' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" prop="fromOrderid" label="来自订单ID" />
        <el-table-column align="center" width="280" prop="fromOrderNo" label="来自订单" />
        <el-table-column align="center" width="160" prop="flag" label="福卡状态">
          <template #default="scope">
            <el-tag>
              {{ scope.row.flag === -2 ? '待领卡'
                : scope.row.flag === -1 ? '待开卡'
                  : scope.row.flag === 0 ? '待兑换'
                    : scope.row.flag === 1 ? '已兑换'
                      : scope.row.flag === -20 ? '退款'
                        : scope.row.flag === -3 ? '下单退款'
                          : scope.row.flag === -4 ? '福卡转增'
                            : scope.row.flag === -5 ? '福卡转增作废' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column header-align="center" width="300" prop="desp" label="备注" />
        <el-table-column align="center" width="240" prop="addTime" label="添加时间" />
        <el-table-column align="center" width="240" prop="updateTime" label="修改时间" />
        <!-- <el-table-column align="center" label="操作" fixed="right" min-width="220">
          <template #default="">
            <div style="line-height: 42px;">
              <el-button type="info" @click="SkipPage('fukaactUserIndex')">
                用户列表
              </el-button>
              <el-button type="info" @click="SkipPage('fukaUserPrize')">
                奖品列表
              </el-button>
              <el-button type="info" @click="SkipPage('fukaUserZhuli')">
                集福助力记录
              </el-button>
            </div>
          </template>
        </el-table-column> -->
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.scrollbar-flex-content {
  display: flex;
  max-width: 300px;

  .scrollbar-flex-img {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 60px;
    margin-right: 10px;
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;

  div {
    text-align: left;
  }
}
</style>
